{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Parameter inference using neural networks\n",
    "\n",
    "Tom Charnock\n",
    "\n",
    "Institut d'Astrophysique de Paris\n",
    "<br><br><br>\n",
    "\n",
    "<div class=\"row\">\n",
    "    <div style=\"float: left; width: 20%; padding-right: 70px; padding-top:35px\">\n",
    "        <img src=\"../figures/SU.png\" alt=\"Sorbonne Université\" style=\"width:80%;\"/>\n",
    "    </div>\n",
    "    <div style=\"float: left; width: 20%; padding: 0px;\">\n",
    "        <img src=\"../figures/ANR.png\" alt=\"ANR\" style=\"width:40%;\"/>\n",
    "    </div>\n",
    "    <div style=\"float: left; width: 20%; padding: 5px;\">\n",
    "        <img src=\"../figures/IAP.png\" alt=\"IAP\" style=\"width:40%;\"/>\n",
    "    </div>\n",
    "    <div style=\"float: left; width: 20%; padding: 5px;\">\n",
    "        <img src=\"../figures/CNRS.png\" alt=\"CNRS\" style=\"width:30%;\"/>\n",
    "    </div>\n",
    "    <div style=\"float: left; width: 20%; padding: 5px;\">\n",
    "        <img src=\"../figures/Aquila.png\" alt=\"Aquila\" style=\"width:60%;\"/>\n",
    "    </div>\n",
    "</div> "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "#### Joint distribution of parameters, $\\boldsymbol{\\theta}$, and possible data, ${\\bf d}$,<br> which can be generated by a model, $\\mathcal{M}:\\boldsymbol{\\theta}\\to{\\bf d}$\n",
    "<br>\n",
    "<div style=\"float:left;width:65%\">\n",
    "<img style=\"max-width:100%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/likelihood_posterior.svg\">\n",
    "</div>\n",
    "<div style=\"float:left;width:35%\">\n",
    "<div style=\"position:absolute;left:55%;top:25%\">$$\n",
    "\\small\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf d},\\mathcal{M}) = \\frac{\\mathcal{L}({\\bf d}|\\boldsymbol{\\theta},\\mathcal{M})p(\\boldsymbol{\\theta}|\\mathcal{M})}{p({\\bf d}|\\mathcal{M})}\n",
    "$$\n",
    "</div>\n",
    "<br><br>\n",
    "<b>Obligatory Bayes Theorem</b>\n",
    "<br><br>\n",
    "<b>Likelihood</b>\n",
    "    $$\\mathcal{L}({\\bf d}|\\boldsymbol{\\theta}^*,\\mathcal{M})$$\n",
    "<br>\n",
    "<b>Posterior</b>\n",
    "    $$\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf d}^*,\\mathcal{M})$$\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Parameter inference using neural networks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### It's common to try and predict model parameters by regression\n",
    "<br>\n",
    "<img style=\"max-width:65%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/neural_network_t.svg\">\n",
    "<br>\n",
    "<center>\n",
    "$$\\mathbb{NN}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha}) : {\\bf d}\\rightarrow\\boldsymbol{\\tau}$$\n",
    "An approximation to a model, $\\mathcal{M} : {\\bf d}\\rightarrow\\boldsymbol{\\theta}$\n",
    "</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## How likely are we to obtain<br> any particular output from the network\n",
    "<br><br>\n",
    "<img style=\"max-width:95%;max-height:80%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/Flat_likelihood_theta.svg\">\n",
    "<br>\n",
    "$$\\Lambda(\\boldsymbol{\\theta},\\boldsymbol{\\tau})\\propto-\\ln\\mathcal{L}(\\boldsymbol{\\theta}|{\\bf d},\\boldsymbol{\\omega},\\boldsymbol{\\alpha})$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### The posterior becomes the posterior predictive density\n",
    "\n",
    "$$\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf d}) = \\int d\\boldsymbol{\\omega}d\\boldsymbol{\\alpha}~\\mathcal{L}(\\boldsymbol{\\theta}|{\\bf d},\\boldsymbol{\\omega},\\boldsymbol{\\alpha})\\mathcal{P}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha})$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<div style=\"float:left;width:45%\">\n",
    "    $\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf d})$ - <u>Posterior predictive density</u><br>\n",
    "    How likely are the true parameters given some data?<br><br>\n",
    "</div>\n",
    "<div style=\"float:left;width:10%\"><br></div>\n",
    "<div style=\"float:left;width:45%\">\n",
    "    $\\mathcal{L}(\\boldsymbol{\\theta}|{\\bf d},\\boldsymbol{\\omega},\\boldsymbol{\\alpha})$ - <u>Likelihood</u><br>\n",
    "    How likely are the parameters to be generated by a particular network?\n",
    "</div>\n",
    "<div style=\"float:left;width:100%\">\n",
    "    $\\mathcal{P}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha})$ - <u>Probability density</u><br>\n",
    "    What is the probability of obtaining a particular network with particular parameter values?\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### The chance of getting any parameter value from any network given some data is <i>almost</i> equal\n",
    "<img style=\"max-width:80%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/likelihood_theta.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### So all the information comes from prior knowledge of the weights and hyperparameters\n",
    "\n",
    "$$\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf d})= \\int d\\boldsymbol{\\omega}d\\boldsymbol{\\alpha}~\\mathcal{L}(\\boldsymbol{\\theta}|{\\bf d},\\boldsymbol{\\omega},\\boldsymbol{\\alpha})\\mathcal{P}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha})$$\n",
    "<br><br>\n",
    "<img style=\"max-width:100%;max-height:50%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/posterior_predictive_theta.gif\">\n",
    "<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Where does the information about the weights and hyperparameters come from?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Training and validation data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "$$\\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train}\\equiv\\{{\\bf d}^\\textrm{train}_i, \\boldsymbol{\\theta}^\\textrm{train}_i|i\\in[1,n_\\textrm{train}]\\}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "$$\\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{val}\\equiv\\small\\{{\\bf d}^\\textrm{val}_i, \\boldsymbol{\\theta}^\\textrm{val}_i|i\\in[1,n_\\textrm{val}]\\}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### The prior distribution of weights and hyperparameters<br> becomes the posterior\n",
    "$$\\begin{align*}\n",
    "    &\\small\\mathcal{P}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha}|\\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train},\\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{val})\\propto\\\\\n",
    "    &\\small\\phantom{hello}\\mathcal{L}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha}|\\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train},\\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{val})p(\\boldsymbol{\\omega},\\boldsymbol{\\alpha})\n",
    "\\end{align*}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# <i>Training</i> a network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "#### What are the maximum likelihood estimates of the weights?\n",
    "<br>\n",
    "<img style=\"max-width:80%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/mle_net_theta.gif\">\n",
    "\n",
    "$$\\boldsymbol{\\omega}^\\textrm{MLE}=\\underset{\\boldsymbol{\\omega}}{\\textrm{argmax}}\\left[\\mathcal{L}(\\boldsymbol{\\{}\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train}|\\boldsymbol{\\{}{\\bf d}\\boldsymbol{\\}}^\\textrm{train}, \\boldsymbol{\\omega}, \\boldsymbol{\\alpha}^*)\\right]$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## We degenerate the posterior\n",
    "<br>\n",
    "$$\\begin{align*}\n",
    "    \\tiny\\mathcal{P}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha}|\\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train})\\propto&\\tiny~\\mathcal{L}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha}|\\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train})p(\\boldsymbol{\\omega},\\boldsymbol{\\alpha})\\\\\n",
    "    \\tiny\\to&\\tiny~\\delta(\\boldsymbol{\\omega}-\\boldsymbol{\\omega}^\\textrm{MLE},\\boldsymbol{\\alpha}-\\boldsymbol{\\alpha}^*)\n",
    "    \\tiny\\\\\n",
    "    \\tiny\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf d}) \\propto&\\tiny~\\int d\\boldsymbol{\\omega}d\\boldsymbol{\\alpha}\\mathcal{L}(\\boldsymbol{\\theta}|{\\bf d},\\boldsymbol{\\omega},\\boldsymbol{\\alpha})\\delta(\\boldsymbol{\\omega}-\\boldsymbol{\\omega}^\\textrm{MLE},\\boldsymbol{\\alpha}-\\boldsymbol{\\alpha}^*)\\\\\n",
    "    \\tiny=&\\tiny~\\delta(\\boldsymbol{\\tau})\n",
    "\\end{align*}$$<br>\n",
    "<img style=\"max-width:100%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/posterior_predictive_mle.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### All predictions are (unknowably incorrect) estimates<br><br>\n",
    "<img style=\"max-width:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/nn_b_theta.gif\"><br>\n",
    "$$\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf d})=\\delta(\\boldsymbol{\\tau})$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### There is no way to interpret how close $\\boldsymbol{\\tau}$ is to $\\boldsymbol{\\theta}$...\n",
    "<img style=\"max-width:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/likelihood_theta.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Local maximum likelihood estimates\n",
    "<br>\n",
    "<img style=\"max-width:80%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/wrong_maxima_theta.gif\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Are there better methods?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Variational inference\n",
    "<img style=\"max-width:80%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/VB_theta.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "$$\\tiny\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf d})=\\int d\\boldsymbol{\\omega}d\\boldsymbol{\\nu}d\\boldsymbol{\\alpha}~\\mathcal{L}(\\boldsymbol{\\theta}|{\\bf d},\\boldsymbol{\\omega},\\boldsymbol{\\alpha})\\mathcal{Q}(\\boldsymbol{\\omega}|\\boldsymbol{\\nu}, \\boldsymbol{\\alpha}, \\boldsymbol{\\{}{\\bf d}, \\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train})p(\\boldsymbol{\\nu},\\boldsymbol{\\alpha})$$\n",
    "<br>\n",
    "<img style=\"max-width:80%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/vi_b_theta.gif\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Still depends on fixed weights in the complex likelihood surface<br> and choice of variational distribution\n",
    "<br>\n",
    "<img style=\"max-width:110%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/wrong_variational.svg\">\n",
    "$$\\begin{align*}\n",
    "\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf d})&=\\int d\\boldsymbol{\\omega}d\\boldsymbol{\\nu}d\\boldsymbol{\\alpha}~\\mathcal{L}(\\boldsymbol{\\theta}|{\\bf d},\\boldsymbol{\\omega},\\boldsymbol{\\alpha})\\mathcal{Q}(\\boldsymbol{\\omega}|\\boldsymbol{\\nu}, \\boldsymbol{\\alpha}, \\boldsymbol{\\{}{\\bf d}, \\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train})\\\\\n",
    "&\\phantom{hellohello}\\times\\delta(\\boldsymbol{\\nu}-\\boldsymbol{\\nu}^\\textrm{MLE}, \\boldsymbol{\\alpha}-\\boldsymbol{\\alpha}^*)\\\\\n",
    "&=\\int d\\boldsymbol{\\omega}~\\mathcal{L}(\\boldsymbol{\\theta}|{\\bf d},\\boldsymbol{\\omega}, \\boldsymbol{\\alpha}^*)\\mathcal{Q}(\\boldsymbol{\\omega}|\\boldsymbol{\\nu}^\\textrm{MLE}, \\boldsymbol{\\alpha}^*, \\boldsymbol{\\{}{\\bf d}, \\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train}).\n",
    "\\end{align*}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# Bayesian neural networks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "<img style=\"max-width:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/Bayes_theta.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Approximate the posterior distribution of weights and hypermeters\n",
    "\n",
    "$$\\begin{align*}\n",
    "\\small\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf d}) =&\\small~\\int d\\boldsymbol{\\omega}d\\boldsymbol{\\alpha}~\\mathcal{L}(\\boldsymbol{\\theta}|{\\bf d}, \\boldsymbol{\\omega}, \\boldsymbol{\\alpha})\\mathcal{P}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha}|\\boldsymbol{\\{}{\\bf d}, \\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train})\\\\\n",
    " \\propto&~\\int d\\boldsymbol{\\omega}d\\boldsymbol{\\alpha}~\\mathcal{L}(\\boldsymbol{\\theta}|{\\bf d}, \\boldsymbol{\\omega}, \\boldsymbol{\\alpha})\\\\\n",
    " &\\phantom{hello}\\times \\prod_i^{n_\\textrm{train}}\\mathcal{L}(\\boldsymbol{\\theta}^\\textrm{train}_i|{\\bf d}_i^\\textrm{train}, \\boldsymbol{\\omega},\\boldsymbol{\\alpha})p(\\boldsymbol{\\omega},\\boldsymbol{\\alpha}).\\phantom{hello}\n",
    "\\end{align*}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "# Everything is dependent on the training data!\n",
    "\n",
    "<br>\n",
    "\n",
    "Classical network : $\\tiny \\mathcal{P}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha}|\\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train}) \\to \\delta(\\boldsymbol{\\omega}-\\boldsymbol{\\omega}^\\textrm{MLE},\\boldsymbol{\\alpha}-\\boldsymbol{\\alpha}^*)$\n",
    "\n",
    "Variational inference : $\\tiny \\mathcal{P}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha}|\\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train}) = \\mathcal{Q}(\\boldsymbol{\\omega}|\\boldsymbol{\\nu}^\\textrm{MLE}, \\boldsymbol{\\alpha}^*, \\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train})$\n",
    "\n",
    "Bayesian networks : $\\tiny \\mathcal{P}(\\boldsymbol{\\omega},\\boldsymbol{\\alpha}|\\boldsymbol{\\{}{\\bf d},\\boldsymbol{\\theta}\\boldsymbol{\\}}^\\textrm{train}) =\\prod_i^{n_\\textrm{train}}\\mathcal{L}({\\bf t}^\\textrm{train}_i|\\boldsymbol{\\theta}^\\textrm{train}_i,\\boldsymbol{\\omega},\\boldsymbol{\\alpha})p(\\boldsymbol{\\omega},\\boldsymbol{\\alpha})$\n",
    "\n",
    "<img style=\"max-width:80%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/posterior_predictive_wrong_theta.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## If the training data, network optimisation, or (pretty much) anything else is not perfect, the inference will be biased"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Parameter inference with summaries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Consider when the likelihood of some data, $\\mathcal{L}({\\bf d}|\\boldsymbol{\\theta})$ is unknown"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "#### Summarise the data, $\\mathscr{f}:{\\bf d}\\to{\\bf t}$, to have a nicer likelihood for the model $\\mathcal{M}\\otimes\\mathscr{f}:\\boldsymbol{\\theta}\\to{\\bf t}$\n",
    "<br><br>\n",
    "$$\n",
    "\\mathcal{P}(\\boldsymbol{\\theta}|{\\bf t},\\mathcal{M}\\otimes\\mathscr{f}) = \\frac{\\mathcal{L}({\\bf t}|\\boldsymbol{\\theta},\\mathcal{M}\\otimes\\mathscr{f})p(\\boldsymbol{\\theta}|\\mathcal{M})}{p({\\bf t}|\\mathcal{M}\\otimes\\mathscr{f})}\n",
    "$$\n",
    "\n",
    "#### The data no longer enters the inference, only the summaries"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### We can use a neural network as this summarising function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "#### But the likelihood of the summaries is still difficult in this situtation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Forward modelling"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Method 1 : <br><br>Likelihood-free inference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Use the same neural network to summarise observations and simulations\n",
    "<img style=\"max-width:100%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/physical_model_nn.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Generate simulations drawn from prior and summarise\n",
    "<img style=\"max-width:100%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/tsims.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Make a kernel density estimate (or use ABC) on the simulations\n",
    "<br>\n",
    "<img style=\"max-width:80%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/LFI_t.svg\">\n",
    "<b>A slice through at the summarised observation is the approximate posterior</b>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<b>Posterior distribution of galaxy counts and fluxes in fields</b><br><br>\n",
    "<img style=\"max-width:70%;max-height:70%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/ABC_IMNN_noted.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### We're still biased by the form of the density estimator we use, and by the number of simulations we use for approximating the posterior"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Method 2 : <br><br>Infer the data, physics and neural network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Need a good control on the likelihood of weights and hyperparameters\n",
    "\n",
    "#### - remove degeneracies\n",
    "#### - force to be convex\n",
    "#### - etc."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "# Use neural physical engines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<div style=\"float:left;width:45%\">\n",
    "    <img style=\"max-width:100%;max-height:100%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/convolutional_network.svg\">\n",
    "</div>\n",
    "<div style=\"float:left;width:10%\"><br></div> \n",
    "<div style=\"float:left;width:45%\">\n",
    "    <img style=\"max-width:100%;max-height:100%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/spherical_convolutional_network.svg\"><br><br>\n",
    "</div>\n",
    "<div style=\"float:left;width:45%\">\n",
    "    <img style=\"max-width:100%;max-height:100%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/receptive_field.svg\">\n",
    "</div>\n",
    "<div style=\"float:left;width:10%\"><br></div>\n",
    "<div style=\"float:left;width:45%\">\n",
    "    <img style=\"max-width:100%;max-height:100%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/kernels.svg\">\n",
    "</div><br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "<img style=\"max-width:80%;max-height:60%;display:block;height:auto;margin:auto;float:none!important;\" src=\"../figures/NBM_square.svg\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Conclusions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "### Doing parameter estimation with neural networks is not as easy as predicting parameter values\n",
    "\n",
    "### We can still use results from neural networks as highly informative summaries\n",
    "\n",
    "### We can also infer all parameters of the forward model, including network to get unbiased results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Questions\n",
    "\n",
    "### When do we say we trust a neural network?\n",
    "\n",
    "### How approximate is too approximate?\n",
    "\n",
    "### Are using neural networks for parameter estimation worse than using other summary statistics?"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Diaporama",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "rise": {
   "leap_motion": {
    "naturalSwipe": true,
    "pointerColor": "#d80000",
    "pointerOpacity": 0.5
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
